package com.wzy.new_energy_car_platform.utils;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.sql.PseudoColumnUsage;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtUtils {
    //2小时过期
    private static final long EXPIRATION_TIME = 2 * 60 * 60 * 1000;

    //密钥
    private static final String SECRET = "WZY_SECRET";

    // 生成token
    public static String generateToken(String phoneNumber,String password) {
        Map<String,Object> map = new HashMap();
        map.put("phoneNumber",phoneNumber);
        map.put("password",password);
        Date now = new Date();
        Date expiration = new Date(now.getTime() + EXPIRATION_TIME );
        return Jwts.builder()
                .setHeaderParam("type","JWT")
//                .setSubject(phoneNumber)
                .setHeader(map)
                .setIssuedAt(now)
                .setExpiration(expiration)
                .signWith(SignatureAlgorithm.HS512, SECRET)
                .compact();
    }


    //解析token 获取手机号和密码
    public static Map<String, Object> getUser(String token){
        return Jwts.parser()
                .setSigningKey(SECRET)
                .parseClaimsJws(token)
                .getHeader();
    }
}
